<template>
	<view>
		<view class="cu-list grid col-2 no-border">

			<view class="cu-item text-left">

				<view style="margin-left: 9px;">
					<view class="text-xsl row">{{type}}</view>
				</view>

			</view>
			<view class="cu-item text-right">

				<view style="margin-left: 9px;">
					<view class="text-lg row margin-right-sm">{{type1_use}}</view>
					<view class="text-xs row text-gray margin-right-sm">{{type}}</view>
					<view class="text-xs row text-gray margin-right">{{i18n.cash.available}}</view>
				</view>
				<view style="margin-left: 9px;">
					<view class="text-lg row margin-right-sm">{{type1_freeze}}</view>
					<view class="text-xs row text-gray margin-right-sm">{{type}}</view>
					<view class="text-xs row text-gray margin-right">{{i18n.cash.Freeze}}</view>
				</view>
				<view style="margin-left: 9px;">
					<view class="text-lg row margin-right-sm">{{type1_total}}</view>
					<view class="text-xs row text-gray margin-right-sm">{{type}}</view>
					<view class="text-xs row text-gray margin-right">{{i18n.cash.all}}</view>
				</view>
			</view>
		</view>

		<form @submit="" @reset="">
			<view class="cu-form-group ">
				<view class="title">{{i18n.cash.cashtype}}</view>
				<picker @change="PickerChange" :value="type" :range="picker">
					<view class="picker">
						{{type}}
					</view>
				</picker>
			</view> 
			<view class="cu-form-group">
				<view class="title">{{i18n.cash.addr}}</view>
				<input type="text" :placeholder="i18n.cash.inputaddr" v-model="form.userwallet"  @tap="chight" @blur="lost"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">{{i18n.cash.number}}</view>
				<input type="number" :placeholder="i18n.cash.inputnumber" v-model="form.number" @tap="chight" @blur="lost"></input>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">提币备注</view>
				<input placeholder="选填" name="input"></input>
			</view> -->
		</form>
		<view class="cu-list grid col-1 no-border tij"  :style="{position: inc ?'static' : 'absolute'}">
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-yellow lg" @tap="submit">{{i18n.cash.confirm}}</button>
			</view>

			<view class="flex-sub  ">
				<view class="solid-bottom text-xs padding">
					{{i18n.cash.info}}
				</view>
			</view>
		</view>
		<key-words :show_key="show_key" :show_subTitle="false" :price="'1.00'" @closeFuc="closeFuc" @getPassword="getPassword"></key-words>
	</view>
	
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				// picker: ['BTC', 'ETH', 'EMP'],
				type: '',
				form:{
					number:'',
					userwallet:'',
					ptype1:''
				},
				inc: false, 
				show_key: false,
				check:false,
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
				myconfig: state => state.user.myconfig
			}),
			hasLogin() {
				return this.userinfo.id ? true : false
			},
			rule(){
				return this.myconfig.cashwall;
			},
			cointype2(){
				return this.myconfig.cointype2;
			},
			picker() {
				let arr = [];
				for (let key in this.rule) {
					arr.push(key)
				}
				return arr;
			},
			type1_use() {
				return this.userinfo[this.rule[this.type]];
			},
			 
			type1_freeze() {
				return this.userinfo[this.rule[this.type] + 'freeze'];
			},
			 
			type1_total(){
				let wall=this.rule[this.type];
				return this.accAdd(this.userinfo[wall],this.userinfo[wall + 'freeze'])
			},  
			i18n() {
				return this.$t('message');
			},
		},
		methods: {
			...mapActions(['getUserinfo']),
			PickerChange(e) {
				this.type = this.picker[e.detail.value];
				this.form.ptype1=this.cointype2[this.type];
			},
			//加法
			accAdd(arg1, arg2) {
				var r1, r2, m, n;
				try {
					r1 = arg1.toString().split(".")[1].length
				} catch (e) {
					r1 = 0
				}
				try {
					r2 = arg2.toString().split(".")[1].length
				} catch (e) {
					r2 = 0
				}
				m = Math.pow(10, Math.max(r1, r2));
				//last modify by deeka
				//动态控制精度长度
				n = (r1 >= r2) ? r1 : r2;
				var ree = ((arg1 * m + arg2 * m) / m).toFixed(n);
				return ree > 0 ? ree : 0;
			},
			submit(){
				if(!this.check){
					this.show_key = true;
					return 1; 
				}  
				this.$http.post('user.Cash.edit', this.form).then(res => {
					this.$api.msg(this.i18n.charge.successINFO);
					this.getUserinfo()
					setTimeout(() => {
						this.$api.prePage()
					}, 800)
				})
			},
			chight() {
				this.inc=true;
			},
			lost(){
				this.inc=false;
			},
			closeFuc() {
				this.show_key = false;
			},
			//密码输入完需要执行的方法
			getPassword(msg) {
				//msg:密码   例如:{msg:123456}
				console.log(msg)
				let data={
					password2:msg.password
				}
				this.$http.post('user.checkpass', data).then(res => {
					console.log(res)
					if(res.code==1){
						this.check=true;
						this.submit();
					}else{
						this.$api.msg(res.msg); 
					}  
				})
				this.closeFuc();
			},
		},
		onShow() {
			uni.setNavigationBarTitle({
            	title: this.i18n.cash.title
        	});
			this.getUserinfo()
			this.type = this.picker[0]; 
			this.form.ptype1=this.cointype2[this.type];
			this.form.userwallet=this.userinfo.imtoken;
		},
	}
</script>

<style>
	.row {
		display: inline;
	}
	.tij{
		bottom: 0px;
		 
		width: -webkit-fill-available;
	}
	 
</style>
